;************************************************************************************
crccheck force
;************************************************************************************

;--------------------------------
;Includes
   !include "MUI.nsh"
   !include "Sections.nsh"
   !include "WinMessages.nsh"
   !include "logiclib.nsh"


;Macros

;--------------------------------
!define MUSINK_VERSION "1.00"
!define NAME "Musink ${MUSINK_VERSION}"
!define FNAME "Musink-${MUSINK_VERSION}"
!define DISPLAYNAME "Musink ${MUSINK_VERSION}"
!define VERSIONDATE "${__DATE__}"

;Version Information
  VIProductVersion "1.0.0.0"
  VIAddVersionKey  "ProductName" "Musink"
  VIAddVersionKey  "Comments" "Freeing the power of choice."
  VIAddVersionKey  "CompanyName" "Quill Pig Productions"
  VIAddVersionKey  "FileDescription" "${NAME}"
  VIAddVersionKey  "FileVersion" "${MUSINK_VERSION}"
  VIAddVersionKey  "LegalCopyright" "Quill Pig Productions"


;--------------------------------
;General

  ;Name and file
  Name "Musink"
  OutFile "Musink_1.00_setup.exe"

  ;Default installation folder
  InstallDir "$PROGRAMFILES\Musink"
  
  ;Get installation folder from registry if available
  InstallDirRegKey HKCU "Software\Musink" ""

;--------------------------------
;Interface Settings
  !define MUI_FINISHPAGE_NOAUTOCLOSE
  !define MUI_UNFINISHPAGE_NOAUTOCLOSE
  !define MUI_ABORTWARNING
  !define MUI_LICENSEPAGE_CHECKBOX
  !define MUI_LICENSEPAGE_CHECKBOX_TEXT "I agree."
  !define MUI_HEADERIMAGE
  !define MUI_HEADERIMAGE_RIGHT
	!define MUI_HEADERIMAGE_BITMAP "musink.bmp"
	!define MUI_WELCOMEFINISHPAGE_BITMAP "welcome.bmp"
;--------------------------------
;Pages
	!insertmacro MUI_PAGE_WELCOME
  !insertmacro MUI_PAGE_LICENSE "license.txt"
  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_INSTFILES
  
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES
  
;--------------------------------
;Languages
 
  !insertmacro MUI_LANGUAGE "English"

;--------------------------------
;Installer Sections

Section "Musink (required)"

  SectionIn RO
  
  ; Set output path to the installation directory.
  SetOutPath $INSTDIR
  
  ; Put file there
  ;File "quillpig.ico"
  File "dbm.exe"
	File "FTP.config"
	File "port.config"
	File "server.config"
	File "library.xml"

  
  ; Write the installation path into the registry
  WriteRegStr HKLM SOFTWARE\Musink "Install_Dir" "$INSTDIR"
  WriteRegDWORD HKLM "Software\Musink" "musink" "1"
  WriteRegStr HKLM "Software\Musink" "InstName" "${NAME}"
  WriteRegStr HKLM "Software\Musink" "InstVer" "${MUSINK_VERSION}"
  WriteRegStr HKLM "Software\Musink" "InstallDir" "$INSTDIR"
  WriteRegStr HKLM "Software\Musink" "StartMenuDir" "$startmenudir"
  
  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${NAME}" "DisplayName" "${DISPLAYNAME} (Remove Only)"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${NAME}" "DisplayIcon" "$INSTDIR\dbm.exe,	0"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${NAME}" "InstallLocation" "$INSTDIR"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${NAME}" "UninstallString" "$INSTDIR\Uninstall.exe"
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${NAME}" "NoModify" "1"
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${NAME}" "NoRepair" "1"
	
  nsSCM::Install /NOUNLOAD "musink" "Musink Database Manager" 16 2 "$INSTDIR\dbm.exe" "" "" "" ""
  nsSCM::Start /NOUNLOAD "musink"

  WriteUninstaller "uninstall.exe"
  
SectionEnd

; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"

  CreateDirectory "$SMPROGRAMS\Musink"
  CreateShortCut "$SMPROGRAMS\Musink\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  CreateShortCut "$SMPROGRAMS\Musink\Musink Database Manager (debug).lnk" "$INSTDIR\dbm.exe" "" "$INSTDIR\dbm.exe" 0
  
SectionEnd

;--------------------------------
;Descriptions

  ;Language strings
  ;LangString DESC_SecDummy ${LANG_ENGLISH} "A test section."

  ;Assign language strings to sections
;  !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
;    !insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_SecDummy)
;  !insertmacro MUI_FUNCTION_DESCRIPTION_END

;--------------------------------
;Uninstaller Section

Section "Uninstall"

  ;ADD YOUR OWN FILES HERE...

  Delete "$INSTDIR\Uninstall.exe"

  RMDir "$INSTDIR"

  DeleteRegKey /ifempty HKCU "Software\Modern UI Test"

SectionEnd









!macro checkrightspoweruser
 UserInfo::GetAccountType
 pop $R0

 ${if} $R0 != "Admin"
 ${andif} $R0 != "Power"

  MessageBox MB_OK|MB_ICONSTOP "This program can only be run by power users or higher." IDOK
  abort
 ${endif}
!macroend

!macro checkrightsnormaluser
 UserInfo::GetAccountType
 pop $R0

 ${if} $R0 != "Admin"
 ${andif} $R0 != "Power"
 ${andif} $R0 != "Normal"

  MessageBox MB_OK|MB_ICONSTOP "This program can only be run by normal users or higher." IDOK
  abort
 ${endif}
!macroend


Function GetWindowsVersion
 Push $R0
 Push $R1

 ClearErrors

 ReadRegStr $R0 HKLM \
 "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion

 IfErrors 0 lbl_winnt

 ; we are not NT
 ReadRegStr $R0 HKLM \
 "SOFTWARE\Microsoft\Windows\CurrentVersion" VersionNumber

 StrCpy $R1 $R0 1
 StrCmp $R1 '4' 0 lbl_error

 StrCpy $R1 $R0 3

 StrCmp $R1 '4.0' lbl_win32_95
 StrCmp $R1 '4.9' lbl_win32_ME lbl_win32_98

 lbl_win32_95:
  StrCpy $R0 '95'
 Goto lbl_done

 lbl_win32_98:
  StrCpy $R0 '98'
 Goto lbl_done

 lbl_win32_ME:
  StrCpy $R0 'ME'
 Goto lbl_done

 lbl_winnt:

 StrCpy $R1 $R0 1

 StrCmp $R1 '3' lbl_winnt_x
 StrCmp $R1 '4' lbl_winnt_x

 StrCpy $R1 $R0 3

 StrCmp $R1 '5.0' lbl_winnt_2000
 StrCmp $R1 '5.1' lbl_winnt_XP
 StrCmp $R1 '5.2' lbl_winnt_2003 lbl_error

 lbl_winnt_x:
  StrCpy $R0 "NT $R0" 6
 Goto lbl_done

 lbl_winnt_2000:
  Strcpy $R0 '2000'
 Goto lbl_done

 lbl_winnt_XP:
  Strcpy $R0 'XP'
 Goto lbl_done

 lbl_winnt_2003:
  Strcpy $R0 '2003'
 Goto lbl_done

 lbl_error:
  Strcpy $R0 ''
 lbl_done:

 Pop $R1
 Exch $R0

FunctionEnd


Function .onInit
  Call GetWindowsVersion
  Pop $R0

  ${if} $R0 != "2000"
  ${andif} $R0 != "XP"
  ${andif} $R0 != "2003"
  MessageBox MB_OK|MB_ICONSTOP "Only Windows 2000/XP/2003 is supported by this installer."
  quit
  ${endif}
  !insertmacro checkrightspoweruser
  System::Call 'kernel32::CreateMutexA(i 0, i 0, t "musinkinstaller") i .r1 ?e'
  Pop $R0
  ${unless} $R0 = 0
  MessageBox MB_OK|MB_ICONEXCLAMATION "Installer already running."
  Abort
  ${endunless}

  ClearErrors
FunctionEnd

Function un.onInit
  !insertmacro checkrightspoweruser
FunctionEnd



;*********************************************************************************
Section "un.Uninstall"
  System::Call 'kernel32::CreateMutexA(i 0, i 0, t "musinkun") i .r1 ?e'
  Pop $R0
  ${unless} $R0 = 0
  MessageBox MB_OK|MB_ICONEXCLAMATION "Uninstaller already running."
  Abort
  ${endunless}

	!insertmacro checkrightspoweruser

  ; Remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${NAME}"
  DeleteRegKey HKLM Software\Musink

	; Remote Service
	nsSCM::Remove /NOUNLOAD "musink" 
	
	SetOutPath $INSTDIR
	
	
  ; Remove shortcuts, if any
  Delete "$SMPROGRAMS\Musink\*.*"

  ; Remove directories used
  RMDir "$SMPROGRAMS\Musink"
  RMDir /r "$INSTDIR"

SectionEnd